package com.yutian.springboot.jdbctemplate.controller;

import com.yutian.springboot.jdbctemplate.biz.StudentDataSourceBiz;
import com.yutian.springboot.jdbctemplate.biz.StudentJdbcBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentJdbcBiz studentJdbcBiz;

    @Autowired
    private StudentDataSourceBiz studentDataSourceBiz;


	@GetMapping("/jdbc/info")
    @ResponseBody
    public Map<String, Object> getJdbcInfo(Long id, HttpServletRequest httpRequest){
        Map<String, Object> res = new HashMap<>();
        List<Map<String, Object>> student = studentJdbcBiz.get(id);
        if(CollectionUtils.isEmpty(student)){
            return res;
        }
        return student.get(0);
    }

    @GetMapping("/datasource/info")
    @ResponseBody
    public Map<String, Object> getDatasourceInfo(Long id, HttpServletRequest httpRequest){
        Map<String, Object> res = new HashMap<>();
        List<Map<String, Object>> student = studentDataSourceBiz.get(id);
        if(CollectionUtils.isEmpty(student)){
            return res;
        }
        return student.get(0);
    }
}
